You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Grid, Typography } from '@mui/material';
  2. import { Box, Container } from '@mui/system';
  3. import { dehydrate, QueryClient } from '@tanstack/react-query';
  4. import { NextPage } from 'next';
  5. import { useTranslation } from 'next-i18next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import Image from 'next/image';
  8. import { useRouter } from 'next/router';
  9. import React from 'react';
  10. import GridItem from '../../components/grid-item/GridItem';
  11. import Loader from '../../components/loader/Loader';
  12. import ProductCard from '../../components/product-card/ProductCard';
  13. import TabContent from '../../components/tab-content/TabContent';
  14. import { useFetchSingleProduct } from '../../hooks/useFetchProductData';
  15. import { getProductData } from '../../requests/products/producDataRequest';
  16. import { useStore, useStoreUpdate } from '../../store/cart-context';
  17. const SingleProduct: NextPage = () => {
  18. const { t } = useTranslation('products');
  19. const { addCartValue } = useStoreUpdate();
  20. const { cartStorage } = useStore();
  21. const router = useRouter();
  22. const { customId } = router.query;
  23. const { data, isLoading } = useFetchSingleProduct(customId);
  24. const addProductToCart = (quantity) => addCartValue(data.product, quantity);
  25. const inCart = cartStorage?.some(
  26. (item) => item.product.customID === data?.product.customID
  27. )
  28. ? true
  29. : false;
  30. if (isLoading) {
  31. return <Loader loading={isLoading} />;
  32. }
  33. return (
  34. <Box
  35. sx={{
  36. display: 'flex',
  37. flexDirection: 'column',
  38. }}
  39. >
  40. <Container>
  41. <Typography
  42. fontFamily={'body1.fontFamily'}
  43. fontSize="32px"
  44. sx={{ mt: 25, height: '100%', color: 'primary.main' }}
  45. >
  46. {data.product.name}
  47. </Typography>
  48. <Grid container spacing={2}>
  49. <Grid sx={{ display: 'flex' }} item md={6} sm={12}>
  50. <Image
  51. src={data.product.image}
  52. alt="product"
  53. width={900}
  54. height={700}
  55. />
  56. </Grid>
  57. <TabContent
  58. description={data?.product.description}
  59. inCart={inCart}
  60. price={data?.product.price}
  61. category={data?.product.category}
  62. addProductToCart={addProductToCart}
  63. />
  64. </Grid>
  65. <Typography
  66. sx={{
  67. mt: { xs: '60px', md: '100px', lg: '150px' },
  68. mb: 5,
  69. color: 'primary.main',
  70. fontSize: '32px',
  71. }}
  72. >
  73. {t('products:similar')}
  74. </Typography>
  75. <Grid container spacing={2}>
  76. {data.similarProducts.map((product) => (
  77. <GridItem key={product._id}>
  78. <ProductCard product={product} />
  79. </GridItem>
  80. ))}
  81. </Grid>
  82. </Container>
  83. </Box>
  84. );
  85. };
  86. export const getServerSideProps = async (context: any) => {
  87. const { params } = context;
  88. const { customId } = params;
  89. const queryClient = new QueryClient();
  90. await queryClient.prefetchQuery(
  91. ['product', customId],
  92. async () => await getProductData(customId)
  93. );
  94. return {
  95. props: {
  96. dehydratatedState: dehydrate(queryClient),
  97. ...(await serverSideTranslations(context.locale, ['products'])),
  98. },
  99. };
  100. };
  101. export default SingleProduct;